package com.hjq.demo.ui.fragment;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.widget.TextView;

import com.hjq.base.BaseAdapter;
import com.hjq.demo.R;
import com.hjq.demo.action.StatusAction;
import com.hjq.demo.app.AppActivity;
import com.hjq.demo.app.TitleBarFragment;
import com.hjq.demo.http.api.ShipmentListApi;
import com.hjq.demo.http.api.UserListApi;
import com.hjq.demo.http.model.HttpData;
import com.hjq.demo.http.model.HttpListData;
import com.hjq.demo.other.NewConfig;
import com.hjq.demo.ui.activity.AddCustomerInformationActivity;
import com.hjq.demo.ui.activity.AddShipmentRecordActivity;
import com.hjq.demo.ui.adapter.ShipmentRecordAdapter;
import com.hjq.demo.widget.StatusLayout;
import com.hjq.http.EasyHttp;
import com.hjq.http.EasyLog;
import com.hjq.http.listener.HttpCallback;
import com.hjq.widget.layout.WrapRecyclerView;
import com.scwang.smart.refresh.layout.SmartRefreshLayout;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

/**
 *    author : Android 轮子哥
 *    github : https://github.com/getActivity/AndroidProject
 *    time   : 2020/07/10
 *    desc   : 加载案例 Fragment
 */
public final class ShipmentRecordFragment extends TitleBarFragment<AppActivity>
        implements OnRefreshLoadMoreListener,
        BaseAdapter.OnItemClickListener, StatusAction {
    private StatusLayout mStatusLayout;
    public static ShipmentRecordFragment newInstance() {
        return new ShipmentRecordFragment();
    }


    private SmartRefreshLayout mRefreshLayout;
    private WrapRecyclerView mRecyclerView;
    private int pageSize =10;
    private int pageNum = 1 ;
    private ShipmentRecordAdapter mAdapter;
    /** 总数量 */
    private int totalNumber;
    @Override
    protected int getLayoutId() {
        return R.layout.shipmen_ecord_fragment;
    }

    @Override
    protected void initView() {
        mRefreshLayout = findViewById(R.id.rl_shipmen_ecord_refresh);
        mRecyclerView = findViewById(R.id.rv_shipmen_ecord_list);
        mStatusLayout = findViewById(R.id.hl_status_hint);
        mAdapter = new ShipmentRecordAdapter(getAttachActivity());
        mAdapter.setOnItemClickListener(this);
        mRecyclerView.setAdapter(mAdapter);

//        TextView headerView = mRecyclerView.addHeaderView(R.layout.picker_item);
//        headerView.setText("我是头部");
//        headerView.setOnClickListener(v -> toast("点击了头部"));
//
//        TextView footerView = mRecyclerView.addFooterView(R.layout.picker_item);
//        footerView.setText("我是尾部");
//        footerView.setOnClickListener(v -> toast("点击了尾部"));

        mRefreshLayout.setOnRefreshLoadMoreListener(this);
    }

    @Override
    protected void initData() {
        analogData();
    }



    /**
     * 模拟数据
     */
    public void analogData() {
        EasyHttp.get(this)
                .api(new ShipmentListApi().setPageSize(pageSize+"")
                        .setPageNum (pageNum+""))
                .request(new HttpCallback<HttpData<ShipmentListApi.Bean>>(this) {

                    @Override
                    public void onSucceed(HttpData<ShipmentListApi.Bean> data) {
                        totalNumber = data.getData().getTotal();
                        if (pageNum ==  1){
                            mAdapter.setData(data.getData().getRows());
                        }else {
                            mAdapter.addData(data.getData().getRows());
                        }
                        if (totalNumber == 0){
                            toast("没有查询到数据");
                            showEmpty();
                        }
                        if (mAdapter.getData().isEmpty() || mAdapter.getData() == null){
                            showEmpty();
                        }
                    }

                    @Override
                    public void onFail(Exception e) {
                        super.onFail(e);
                    }
                });

    }
    /**
     * {@link BaseAdapter.OnItemClickListener}
     *
     * @param recyclerView      RecyclerView对象
     * @param itemView          被点击的条目对象
     * @param position          被点击的条目位置
     */
    @Override
    public void onItemClick(RecyclerView recyclerView, View itemView, int position) {
        AddShipmentRecordActivity.start(getContext(),"3",mAdapter.getItem(position));
    }

    /**
     * {@link OnRefreshLoadMoreListener}
     */

    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {
        pageNum = 1;
        postDelayed(() -> {
            mAdapter.clearData();
            analogData();
            mRefreshLayout.finishRefresh();
        }, 1000);
    }

    @Override
    public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
        pageNum = pageNum+1;
        postDelayed(() -> {
            analogData();
            mRefreshLayout.finishLoadMore();
            mAdapter.setLastPage(mAdapter.getCount() >= 100);
            mRefreshLayout.setNoMoreData(isLastPage());
        }, 1000);
    }


    /**
     * 判断是否是最后一页
     */
    public boolean isLastPage() {
        return Math.ceil((float) totalNumber / pageSize) <= pageNum;
    }
    @Override
    public StatusLayout getStatusLayout() {
        return mStatusLayout;
    }

    public void initmRefreshLayout(){
        onRefresh(mRefreshLayout);
    }
}